home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Ghost 1.0 / source / Ghost ƒ / MSG Shell ƒ / msg sounds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  3.2 KB  |  143 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg sounds.c
  4.  
  5. Purpose:    This module handles playing asynchronous sounds.  Also,
  6.             a modal dialog filter that lets the user cough to continue
  7.             (if an internal microphone is available).
  8.  
  9.  
  10. Ghost -=- a classic word-building challenge
  11. Copyright (C) 1993 Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "msg sounds.h"
  31. #include "msg graphics.h"
  32.  
  33. SndChannelPtr        myChannel;
  34. Handle                MySounds[NUM_SOUNDS];
  35. unsigned char        gSoundToggle;
  36. Boolean                gSoundAvailable;
  37.  
  38. #include "SoundInput.h"
  39.  
  40. static long        gSoundInputRefNum;
  41.  
  42. OSErr OpenTheSoundDevice(void)
  43. {
  44.     OSErr isHuman;
  45.     short meterState = 1;
  46.     
  47.     if (!(isHuman = SPBOpenDevice(0L, siWritePermission, &gSoundInputRefNum)))
  48.         isHuman = SPBSetDeviceInfo(gSoundInputRefNum, siLevelMeterOnOff, &meterState);
  49.     return isHuman;
  50. }
  51.  
  52. void CloseTheSoundDevice(void)
  53. {
  54.     SPBCloseDevice(gSoundInputRefNum);
  55.     gSoundInputRefNum = 0;
  56. }
  57.  
  58. pascal Boolean ProcOFilter(DialogPtr dialog, EventRecord *event, short *item)
  59. {
  60.     unsigned char    theKey;
  61.     OSErr            isHuman;
  62.     short            recordingStatus = 0;
  63.     short            meterLevel = 0;
  64.     unsigned long    totalSamplesToRecord = 0;
  65.     unsigned long    numberOfSamplesRecorded = 0;
  66.     unsigned long    totalMsecsToRecord;
  67.     unsigned long    numberOfMsecsRecorded;
  68.     
  69.     if(gSoundInputRefNum)
  70.     {
  71.         isHuman = SPBGetRecordingStatus(gSoundInputRefNum, &recordingStatus, &meterLevel,
  72.                                     &totalSamplesToRecord, &numberOfSamplesRecorded,
  73.                                     &totalMsecsToRecord,&numberOfMsecsRecorded);
  74.         
  75.         if (meterLevel > 250)
  76.         {
  77.             *item = 1;
  78.             return true;
  79.         }
  80.     }
  81.     
  82.     switch (event->what)
  83.     {
  84.         case mouseDown:
  85.         case keyDown:
  86.         case autoKey:
  87.             *item = 1;
  88.             return TRUE;
  89.             break;
  90.         }
  91.  
  92.     return FALSE;
  93. }
  94.  
  95. void InitSounds(void)
  96. {
  97.     int                i;
  98.     
  99.     gSoundAvailable=(SndNewChannel(&myChannel, 0, 0L, 0L)==noErr);
  100.     if (!gSoundAvailable)
  101.         myChannel=0;
  102.     for (i=0; i<NUM_SOUNDS; i++)
  103.         MySounds[i]=0L;
  104. }
  105.  
  106. void DoSound(int whichSound, Boolean async)
  107. {
  108.     SndCommand        myCommand;
  109.     OSErr            isHuman;
  110.     Str255            tempStr;
  111.     
  112.     if (myChannel!=0)
  113.     {
  114.         SndDisposeChannel(myChannel, TRUE);
  115.         myChannel=0;
  116.     }
  117.     
  118.     whichSound-=1000;
  119.     if ((gSoundToggle) && (gSoundAvailable))
  120.     {
  121.         if (!MySounds[whichSound])
  122.             MySounds[whichSound]=GetResource('snd ', whichSound+1000);
  123.         
  124.         if (MySounds[whichSound])
  125.         {
  126.             if(SndNewChannel(&myChannel, 0, 0L, 0L) != noErr)                    
  127.             {
  128.                 myChannel = 0;
  129.                 gSoundAvailable = FALSE;
  130.             }
  131.             else SndPlay(myChannel, MySounds[whichSound], async);
  132.         }
  133.     }
  134. }
  135.  
  136. void CloseSounds(void)
  137. {
  138.     int                i;
  139.     
  140.     if(myChannel)
  141.         SndDisposeChannel(myChannel, TRUE);
  142. }
  143.